﻿/******************************************************************************
* Author: 671643387@qq.com
* @MR.Luo
*****************************************************************************/

#include "Common.h"
#include "EchoConnectionFactory.h"
#include "EchoServer.h"


EchoServer::EchoServer(dev::net::EventLoop& loop)
	: server_(loop), loop_(loop)
{
    server_.setConnecionFactory(dev::net::TcpConnectionFactoryPtr(new EchoConnectionFactory()));
    server_.setMessageCallback(boost::bind(&EchoServer::onMessage, this, _1, _2));
    server_.setConnectionErrorCallback(boost::bind(&EchoServer::onConnectionError, this, _1));
    server_.setConnectionClosingCallback(boost::bind(&EchoServer::onConnectionClosing, this, _1));
    server_.setConnectionHeartBeatCallback(boost::bind(&EchoServer::onConnectionHeartBeat, this, _1));
    server_.setConnectionWritableCallback(boost::bind(&EchoServer::onConnectionWritable, this, _1));
    server_.setConnectionEstablishedCallback(boost::bind(&EchoServer::onConnectionEstablished, this, _1));

    server_.setServerShutdownCallback(boost::bind(&EchoServer::onServerShutdown, this));
}


EchoServer::~EchoServer()
{
}

void EchoServer::onServerShutdown()
{
    server_.getLoop().stop();
}

void EchoServer::dumpStatics()
{
    struct dev::net::TcpServer::Statics statics;
    server_.getStatics(statics);

    server_.getLogger()->log("====================================");
    server_.getLogger()->log("connIdGen               %d", statics.connIdGen);
    server_.getLogger()->log("status                  %d", statics.status);
    server_.getLogger()->log("isSize                  %d", statics.isSize);
    server_.getLogger()->log("osSize                  %d", statics.osSize);
    server_.getLogger()->log("configWorkers           %d", statics.configWorkers);
    server_.getLogger()->log("runningWorkers          %d", statics.runningWorkers);
    server_.getLogger()->log("masterAsWorker          %d", statics.masterAsWorker);
    server_.getLogger()->log("connInMaster            %d", statics.connInMaster);
    server_.getLogger()->log("masterLoopCount         %lld", statics.masterLoopCount);
    server_.getLogger()->log("totalConns              %d", statics.totalConns);
    server_.getLogger()->log("connHeartTime           %d", statics.connHeartTime);
    server_.getLogger()->log("loopFrameTime           %d", statics.loopFrameTime);
    server_.getLogger()->log("masterloopFrameTime     %d", statics.masterLoopFrameTime);
    server_.getLogger()->log("masterFrameSysTime      %d", statics.masterLoopFrameSysTime);
    server_.getLogger()->log("bytsSend                %lld", statics.bytsSend);
    server_.getLogger()->log("bytsRecv                %lld", statics.bytsRecv);

    for (size_t x = 0; x < statics.workerStatics.size(); ++x)
    {
        server_.getLogger()->log("------%d-------", statics.workerStatics[x].tid);
        server_.getLogger()->log("connInWorker %d", statics.workerStatics[x].connInWorker);
        server_.getLogger()->log("loopCount    %lld", statics.workerStatics[x].loopCount);
        server_.getLogger()->log("frameSysTime %d", statics.workerStatics[x].frameSysTime);
    }
}

void EchoServer::onConnectionEstablished(dev::net::TcpConnectionPtr& conn)
{
}

void EchoServer::onConnectionError(dev::net::TcpConnection& conn)
{
}

void EchoServer::onConnectionClosing(dev::net::TcpConnection& conn)
{
}

void EchoServer::onConnectionHeartBeat(dev::net::TcpConnection& conn)
{
}

void EchoServer::onConnectionWritable(dev::net::TcpConnection& conn)
{
}


void EchoServer::onMessage(dev::net::TcpConnection& conn, dev::net::TcpConnectionInputStream& is)
{
    size_t bytsRecv = is.size();
	char buff[0x4000] = { 0 };
	is >> buff;
    conn.getOutputStream().write(buff, bytsRecv);
}

void EchoServer::close()
{
    dumpStatics();
    server_.close();
}